package com.scs.application.modules.fsd.controller;

import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.fsd.dto.SaveStationDTO;
import com.scs.application.modules.fsd.entity.FsdChest;
import com.scs.application.modules.fsd.service.ChestService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: 柜子管理
 */
@RestController
@RequestMapping("/back_front/chests")
public class ChestController extends CrudController<ChestService, FsdChest> {

    @PostMapping(value = "/createStationAndChestService")
    public RestResult editUser(@RequestBody SaveStationDTO stationDTO) {
        boolean isOk = service.createStationAndChest(stationDTO);
        if (isOk)
            return RestResult.ok("保存成功！");
        return RestResult.error("保存失败");
    }
    @PostMapping(value = "/editStationService")
    public RestResult editStation(@RequestBody SaveStationDTO stationDTO){
        if(service.editStation(stationDTO))
            return RestResult.ok("编辑站点成功");
        return RestResult.error("编辑站点失败");
    }

    @PostMapping(value = "/editChestService")
    public RestResult editChest(@RequestBody Map<String, String> map){
        if(service.editChest(map.get("chestNo"), map.get("id")))
            return RestResult.ok("编辑智能柜成功");
        return RestResult.error("编辑智能柜失败");
    }

    @PostMapping(value = "/createChestService")
    public RestResult createChest(@RequestBody Map<String, String> map) {
        Map<String,String> id = service.createChest(map.get("chestNo"), map.get("stationId"));
        return RestResult.ok("创建智能柜成功",id);
    }

    @PostMapping(value = "/changeStationAndChestNoByChestService")
    public RestResult changeStationAndChestNoByChest(@RequestBody Map<String, String> map) {
        if (service.changeStationAndChestNoByChest(map.get("chestNo"), map.get("stationId"), map.get("chestId")))
            return RestResult.ok("改变智能柜的站点或智能柜号成功");
        return RestResult.error("改变智能柜的站点或智能柜号失败");
    }

    @GetMapping(value = "/getChestsByStationService")
    public RestResult getChestsByStation(@RequestParam("id") String id) {
        List<FsdChest> chests = service.getChestsByStation(id);
        return RestResult.ok("获取站点下的智能柜成功", chests);
    }

    @GetMapping(value = "/getStationByChestService")
    public RestResult getStationByChest(@RequestParam("id") String id) {
        List<FsdChest> chests = service.getStationByChest(id);
        return RestResult.ok("获取智能柜所属的站点成功", chests);
    }

    @GetMapping(value = "/getChestsAndStationsAllService")
    public RestResult getChestsAndStationsAll() {
        return RestResult.ok("获取站点下的智能柜成功",  service.getChestsAndStationsAll());
    }

    /**
     * 添加一个辅柜
     * @param para
     * @return
     */
    @PostMapping("/addChest")
    public RestResult addChest(@RequestBody Map para){
        String stationId = para.get("stationId").toString();
        return service.addChest(stationId);
    }

    /**
     * 移除一个辅柜
     * @param para
     * @return
     */
    @PostMapping("/removeChest")
    public RestResult removeChest(@RequestBody Map para){
        String stationId = para.get("stationId").toString();
        return service.removeChest(stationId);
    }
}

